package com.baizhiedu.rsm.util;

import com.baizhiedu.rsm.dao.ClassMessageDao;
import com.baizhiedu.rsm.dao.ScheduleDao;
import com.baizhiedu.rsm.entity.ClassMessage;
import com.baizhiedu.rsm.entity.Schedule;
import com.baizhiedu.rsm.entity.Student;
import com.baizhiedu.rsm.entity.StudentMessage;
import com.baizhiedu.rsm.service.StudentMessageServcie;
import com.baizhiedu.rsm.service.StudentServcie;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.InputStream;
import java.util.Date;


/**
 * 解析导入用户的工具类
 * @author yjl
 */
public class ParseExcelForStudent {

	/**
	 * 解析导入用户的方法。getCellValue
	 */
	public static void parseAndImportUser(InputStream inputStream, StudentMessageServcie studentMessageServcie, ClassMessageDao classMessageDao, ScheduleDao scheduleDao, Integer sessionClassId){
			try {
				// 将流中数据实例化为 HSSFWorkbook。
				HSSFWorkbook wb = new HSSFWorkbook(inputStream);
				// 获取sheet工作簿
				HSSFSheet sheet = wb.getSheetAt(0);
				int hang = 0;
				// 获取表中所有行
				hang = sheet.getPhysicalNumberOfRows();
				
				for (int h = 1; h < hang; h++) {
					// 获得工作表的行(用户)。
					StudentMessage student = new StudentMessage();
					// 获取当前行数据
					HSSFRow row = sheet.getRow(h);

					String id = GetCellValue.getCellValue(row, 0);
					String className = GetCellValue.getCellValue(row, 1);
					String name = GetCellValue.getCellValue(row, 2);
					String age = GetCellValue.getCellValue(row, 3);
					String education = GetCellValue.getCellValue(row, 4);
					String source = GetCellValue.getCellValue(row, 5);
					String testscore = GetCellValue.getCellValue(row, 6);
					String payment = GetCellValue.getCellValue(row, 7);
					String motivation = GetCellValue.getCellValue(row, 8);
					String technical = GetCellValue.getCellValue(row, 9);
					String stable = GetCellValue.getCellValue(row, 10);
					String schedule = GetCellValue.getCellValue(row, 11);
					String exceptionstate = GetCellValue.getCellValue(row, 12);
					String focus = GetCellValue.getCellValue(row, 13);
					String support = GetCellValue.getCellValue(row, 14);
					String remark = GetCellValue.getCellValue(row, 15);
					String zxteacher = GetCellValue.getCellValue(row, 16);
					String phone = GetCellValue.getCellValue(row, 17);
					String email = GetCellValue.getCellValue(row, 18);
					String wechat = GetCellValue.getCellValue(row, 19);
					String qq = GetCellValue.getCellValue(row, 20);


					//班级id参数的传递可以使用ThredLocal代替
					student.setClassid(sessionClassId);

					if(name == null){
						continue;
					}
					student.setName(name);

					if(age != null){
						student.setAge(age);
					}else {
						student.setAge("");
					}

					if(education != null){
						student.setEducation(education);
					}else {
						student.setEducation("");
					}


					if(source != null){
						student.setSource(source);
					}else {
						student.setSource("");
					}

					if(testscore != null){
						student.setTestscore(Double.parseDouble(testscore));
					}

					if(payment != null){
						student.setPayment(payment);
					}

					if(motivation != null){
						student.setMotivation(motivation);
					}

					if(technical != null){
						student.setTechnical(technical);
					}

					if(stable != null){
						student.setStable(stable);
					}

//					获取进度对应的id
					Schedule schedule1 = scheduleDao.selectBySchedule(schedule);
					if(schedule1 != null){

						student.setSchedule(schedule1.getSid().toString());
					}else {
						student.setSchedule("");
					}

					if(exceptionstate != null){
						student.setExceptionstate(exceptionstate);
					}else {
						student.setExceptionstate("");
					}

					if (focus != null){
						student.setFocus(focus);
					}

					if (support != null){
						student.setSupport(support);
					}

					if(remark != null){
						student.setRemark(remark);
					}else {
						student.setRemark("");
					}

					if(remark != null){
						student.setZxteacher(zxteacher);
					}else {
						student.setZxteacher("");
					}

					if (phone != null){
						student.setPhone(phone);
					}

					if (email != null){
						student.setEmail(email);
					}

					if (wechat != null){
						student.setWechat(wechat);
					}

					if (qq != null){
						student.setQq(qq);
					}
					student.setRbtime(new Date());
					studentMessageServcie.insert(student);
				}
				inputStream.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		

	
}
